#include <iostream>
#include "httplib.h"
#include "contactservice.hpp"
#include "exception.hpp"
using namespace std;

int main()
{

    cout << "---> 服务启动..." << endl;
    httplib::Server srv; // 创建服务端对象
    ContactsServer contactsServer;

    // 服务端Add操作：

    // 这里服务端对应url进行调用把收到的内容也就是proto对象的const模式传给req；即调用这个lamda;还要填充这个res最后除了lamda发给客户端：
    srv.Post("/contacts/add", [&contactsServer](const httplib::Request &req, httplib::Response &res)
             {
                add_contact_req::AddContactRequest myreq;
                 add_contact_res::AddContactResponse myresp;

     try{

                    // 反序列化 request
               if (!myreq.ParseFromString(req.body)) {
                  throw Exception("Parse AddContactRequest error!");
               }
                    // 新增联系人+进行答复填充
              contactsServer.Add(myreq, &myresp);
           
             //进行序列化：
              std::string response_str;
            if (!myresp.SerializeToString(&response_str)) {
                throw Exception("Serialize AddContactResponse error");
            }

            //httplib的response填充：

            res.body=response_str;
            res.status=200;
             res.set_header("Content-Type", "application/protobuf");


                 }

  catch(Exception &e){
             cerr << "---> /contacts/add 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
             res.status = 500;
             myresp.mutable_base_resp()->set_success(false);
             myresp.mutable_base_resp()->set_error_desc(e.what());
              std::string response_str;
            if (myresp.SerializeToString(&response_str)) {
              res.body = response_str;
              res.set_header("Content-Type", "application/protobuf");
            }



                 } });

    // 服务端Del操作：

    srv.Post("/contacts/del", [&contactsServer](const httplib::Request &req, httplib::Response &res)
             {
                del_req::DelContactRequest myreq;
                 del_res::DelContactResponse myresp;

     try{

                    // 反序列化 request
               if (!myreq.ParseFromString(req.body)) {
                  throw Exception("Parse AddContactRequest error!");
               }
                    // 删除联系人+进行答复填充
              contactsServer.Del(myreq, &myresp);
           
             //进行序列化：
              std::string response_str;
            if (!myresp.SerializeToString(&response_str)) {
                throw Exception("Serialize AddContactResponse error");
            }

            //httplib的response填充：

            res.body=response_str;
            res.status=200;
             res.set_header("Content-Type", "application/protobuf");


                 }

  catch(Exception &e){
             cerr << "---> /contacts/del 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
             res.status = 500;
             myresp.mutable_base_resp()->set_success(false);
             myresp.mutable_base_resp()->set_error_desc(e.what());
              std::string response_str;
            if (myresp.SerializeToString(&response_str)) {
              res.body = response_str;
              res.set_header("Content-Type", "application/protobuf");
            }



                 } });



    //查找一个联系人的方法注册：


    srv.Post("/contacts/find-one", [&contactsServer](const httplib::Request &req, httplib::Response &res)
             {
               find_one_req::FindOneContactRequest myreq;
                find_one_res::FindOneContactResponse myresp;

     try{

                    // 反序列化 request
               if (!myreq.ParseFromString(req.body)) {
                  throw Exception("Parse AddContactRequest error!");
               }
                    // 查找一个联系人+进行答复填充
              contactsServer.FindOne(myreq, &myresp);
           
             //进行序列化：
              std::string response_str;
            if (!myresp.SerializeToString(&response_str)) {
                throw Exception("Serialize AddContactResponse error");
            }

            //httplib的response填充：

            res.body=response_str;
            res.status=200;
             res.set_header("Content-Type", "application/protobuf");


                 }

  catch(Exception &e){
             cerr << "---> /contacts/find-one 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
             res.status = 500;
             myresp.mutable_base_res()->set_success(false);
             myresp.mutable_base_res()->set_error_desc(e.what());
              std::string response_str;
            if (myresp.SerializeToString(&response_str)) {
              res.body = response_str;
              res.set_header("Content-Type", "application/protobuf");
            }



                 } });




            
    //获取全部联系人uid与name的注册:

    srv.Get("/contacts/find-all", [&contactsServer](const httplib::Request &req, httplib::Response &res){
          find_all_contacts_resp::FindAllContactsResponse myresp;
         
            try{


             // 查询所有联系人
            contactsServer.FindAll(&myresp);
            // 序列化 resp
            std::string response_str;
            if (!myresp.SerializeToString(&response_str)) {
                throw Exception("Serialize FindAllContactsResponse error");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
            }

            catch(Exception &e){

        cerr << "---> /contacts/find-all 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
             res.status = 500;
             myresp.mutable_base_resp()->set_success(false);
             myresp.mutable_base_resp()->set_error_desc(e.what());
              std::string response_str;
            if (myresp.SerializeToString(&response_str)) {
              res.body = response_str;
              res.set_header("Content-Type", "application/protobuf");
            }

            }


    });

    //注册完方法后进行端口绑定监听：
    srv.listen("0.0.0.0", 8089); 

    return 0;
}